using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Data;
using Dimok;
using Dimok.Exceptions;

namespace Manheim {
	public class ManheimForm : System.Windows.Forms.Form,IManheimForm {
		private System.Windows.Forms.TextBox txtText;
		private System.Windows.Forms.StatusBar stBar;
		private System.Windows.Forms.Button btnLogIn;
		private System.Windows.Forms.Button btnFetchByVIN;
		private System.Windows.Forms.TextBox txtVIN;
		private System.Windows.Forms.TextBox txtMiles;
		private System.ComponentModel.Container components = null;
		ManheimPriceDS dsMP = null;

		#region Constructor
		public ManheimForm() {
			InitializeComponent();
		}
		protected override void Dispose( bool disposing ) {
			if( disposing ) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#endregion

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ManheimForm));
			this.txtText = new System.Windows.Forms.TextBox();
			this.stBar = new System.Windows.Forms.StatusBar();
			this.btnLogIn = new System.Windows.Forms.Button();
			this.btnFetchByVIN = new System.Windows.Forms.Button();
			this.txtVIN = new System.Windows.Forms.TextBox();
			this.txtMiles = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// txtText
			// 
			this.txtText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
									| System.Windows.Forms.AnchorStyles.Left)
									| System.Windows.Forms.AnchorStyles.Right)));
			this.txtText.Location = new System.Drawing.Point(0, 24);
			this.txtText.Multiline = true;
			this.txtText.Name = "txtText";
			this.txtText.Size = new System.Drawing.Size(400, 212);
			this.txtText.TabIndex = 0;
			// 
			// stBar
			// 
			this.stBar.Location = new System.Drawing.Point(0, 238);
			this.stBar.Name = "stBar";
			this.stBar.Size = new System.Drawing.Size(400, 22);
			this.stBar.TabIndex = 1;
			// 
			// btnLogIn
			// 
			this.btnLogIn.Location = new System.Drawing.Point(4, 2);
			this.btnLogIn.Name = "btnLogIn";
			this.btnLogIn.Size = new System.Drawing.Size(48, 20);
			this.btnLogIn.TabIndex = 2;
			this.btnLogIn.Text = "LogIn";
			this.btnLogIn.Click += new System.EventHandler(this.btnLogIn_Click);
			// 
			// btnFetchByVIN
			// 
			this.btnFetchByVIN.Location = new System.Drawing.Point(56, 0);
			this.btnFetchByVIN.Name = "btnFetchByVIN";
			this.btnFetchByVIN.Size = new System.Drawing.Size(48, 20);
			this.btnFetchByVIN.TabIndex = 2;
			this.btnFetchByVIN.Text = "By VIN";
			this.btnFetchByVIN.Click += new System.EventHandler(this.btnFetchByVIN_Click);
			// 
			// txtVIN
			// 
			this.txtVIN.Location = new System.Drawing.Point(107, 0);
			this.txtVIN.Name = "txtVIN";
			this.txtVIN.Size = new System.Drawing.Size(100, 20);
			this.txtVIN.TabIndex = 3;
			this.txtVIN.Text = "KNDUP1312";
			// 
			// txtMiles
			// 
			this.txtMiles.Location = new System.Drawing.Point(216, 0);
			this.txtMiles.Name = "txtMiles";
			this.txtMiles.Size = new System.Drawing.Size(100, 20);
			this.txtMiles.TabIndex = 3;
			this.txtMiles.Text = "50000";
			// 
			// ManheimForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(400, 260);
			this.Controls.Add(this.txtVIN);
			this.Controls.Add(this.txtText);
			this.Controls.Add(this.btnLogIn);
			this.Controls.Add(this.stBar);
			this.Controls.Add(this.btnFetchByVIN);
			this.Controls.Add(this.txtMiles);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "ManheimForm";
			this.Text = "Manheim Server";
			this.Load += new System.EventHandler(this.ManheimForm_Load);
			this.ResumeLayout(false);
			this.PerformLayout();

		}
		#endregion

		private Dimok.IE.IEHolder ieh = new Dimok.IE.IEHolder(new Regex(Dimok.Config.Manheim.Title,RegexOptions.IgnoreCase));
		public Dimok.IE.IEHolder IEH{get{return ieh;}}

		public void InitIE() {
			this.Invoke(new Action(() => { IEH.InitIE(); }));
		}
		private void btnLogIn_Click(object sender, System.EventArgs e) {
			MessageBox.Show(this,"Not Implemented!");
		}

		private void rs_Initialized(Exception exc) {
			txtText.Text = ClientException.GetExceptions(exc);
		}

		private void btnFetchByVIN_Click(object sender, System.EventArgs e) {
			new Action(() => {
				dsMP = new ManheimServer().GetPriceByVIN("SE", txtVIN.Text, int.Parse(txtMiles.Text), "");
			}).BeginInvoke(new AsyncCallback(a => SetText(dsMP.GetXml())), null);
		}

		void SetText(string text) {
			txtText.Invoke(new Action(() => txtText.Text = text));
		}
		#region IManheimForm Members
		delegate void SetStatus(string s);
		public string Status{
			set{
				if (stBar.InvokeRequired) {
					var v = new SetStatus((s) => stBar.Text = s);
					stBar.Invoke(v,value);
				} else stBar.Text = value;
			}
		}
		public Exception Error {
			set {
				txtText.Text += ClientException.GetExceptions(value);
				txtText.SelectAll();
				txtText.Focus();
				txtText.ScrollToCaret();
				txtText.Select(0,0);
				Application.DoEvents();
			}
		}
		#endregion

		private void ManheimForm_Load(object sender, EventArgs e) {
			ieh.ie.Navigate(Dimok.Config.Manheim.LogInURL);
		}
	}
}
